﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using QzEbook.AccessObject;
using QzEbook.Common.Emum;

public partial class admin_module_reg : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["UName"]))
        {
            checkUser(Request["UName"]);
            return;
        }
        if (!String.IsNullOrEmpty(Request["UMail"]))
        {
            checkMail(Request["UMail"]);
        }
        if (!IsPostBack)
        {
        }
    }

    /// <summary>
    /// 检查邮箱是否存在;
    /// </summary>
    private void checkMail(string mail)
    {
        Response.Clear();
        Response.ContentType = "Text/xml";
        string returnValue = string.Format("<root><return>{0}</return></root>", SiteUser.check(RegCheckAction.mail, mail));
        Response.Write(returnValue);
        Response.End();
    }

    /// <summary>
    ///  检查用户是否存在;
    /// </summary>
    private void checkUser(string name)
    {
        Response.Clear();
        Response.ContentType = "Text/xml";
        string returnValue=string.Format("<root><return>{0}</return></root>",SiteUser.check(RegCheckAction.name, name));
        Response.Write(returnValue);
        Response.End();
    }

    /// <summary>
    /// 提交时;
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void sendBtn_Click(object sender, EventArgs e)
    {
        if (Session["RegisterOk"] != null)
        {
            alertM("你刚注册过一个账号");
        }
        SiteUser userInfo = new SiteUser();
        userInfo.Name = userNameTb.Text.Trim();
        //男为1;
        userInfo.Sex = sexRbl.SelectedValue == "1";
        userInfo.Pwd = pwdTb.Text.Trim();
        userInfo.Mail = emailTb.Text.Trim();
        userInfo.Qq = qqTb.Text.Trim();
        userInfo.From = addrTb.Text.Trim();
        userInfo.Tel = telTb.Text.Trim();

        if (userInfo.Name.Length < 3 || pwdTb.Text.Trim().Length < 6 || userInfo.Pwd!=repwdTb.Text.Trim()||userInfo.Mail.Length<5||userInfo.Mail.IndexOf("@")<0||userInfo.Tel.Length!=11)
        {
            alertM("信息填写不完整");
            return;
        }
        bool action=userInfo.insert();
        if (action)
        {
            Session["RegisterOk"] = true;
            string info=string.Format("你的用户<em>{0}</em> 已注册成功,你需要进行<a href=\"Login.xhtml\">登陆</a>",userInfo.Name);
            info = Server.UrlEncode(info);
            Response.Redirect(String.Format("Notice.xhtml?info={0}",info),true);
        }
        else
        {
            alertM("发生问题,用户可能已存在");
        }

    }


    private void alertM(string message)
    {
        if (!Page.ClientScript.IsStartupScriptRegistered("alert"))
        {
            StringBuilder sb=new StringBuilder();
            sb.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
            sb.AppendFormat("alert('{0}')", message);
            sb.AppendLine("</script>");
            Page.ClientScript.RegisterStartupScript(Page.GetType(),"alert", sb.ToString());
        }
    }
}
